package com.zzy.js;

import java.io.FileNotFoundException;
import java.io.FileReader;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import jdk.nashorn.api.scripting.*; 

import org.junit.Test;

/**
 * Hello world!
 *
 */
public class NashornDemo {

	public static void main(String[] args) {
		ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
		try {
			engine.eval("print('Hello World!');");
		} catch (ScriptException e) {
			throw new RuntimeException(e);
		}
	}

	@Test
	public void testFunc() throws FileNotFoundException, ScriptException, NoSuchMethodException {
		String path = NashornDemo.class.getClassLoader().getResource("script.js").getPath();

		ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
		engine.eval(new FileReader(path));
		Invocable invocable = (Invocable) engine;
		Object result = invocable.invokeFunction("fun1", "Peter Parker");
		System.out.println(result);
		System.out.println(result.getClass());
	}

	/**
	 * java静态方法
	 * 
	 * @param name
	 * @return
	 */
	public static String javaFunc(String name) {
		System.out.format("Hi there from Java, %s", name);
		return "greetings from java";
	}

	@Test
	public void testJsCallJava() throws FileNotFoundException, ScriptException, NoSuchMethodException {
		String path = NashornDemo.class.getClassLoader().getResource("script.js").getPath();

		ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
		engine.eval(new FileReader(path));
		Invocable invocable = (Invocable) engine;
		Object result = invocable.invokeFunction("callJava", "Peter Parker");
		System.out.println(result);
	}
	
	/**
	 * java静态方法
	 * 
	 * @param name
	 * @return
	 */
	@SuppressWarnings("restriction")
	public static String javaFuncParam(ScriptObjectMirror person) {
		System.out.println("Full Name is: " + person.callMember("getFullName"));
		return "1111111";
	}
	
	@Test
	public void testJsCallJavaParam() throws FileNotFoundException, ScriptException, NoSuchMethodException {
		String path = NashornDemo.class.getClassLoader().getResource("script.js").getPath();

		ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
		engine.eval(new FileReader(path));
		Invocable invocable = (Invocable) engine;
		Object result = invocable.invokeFunction("callJavaParam");
		System.out.println(result);
	}
	
	
	@Test
	public void testFuncLoadJs() throws FileNotFoundException, ScriptException, NoSuchMethodException {
		String path = NashornDemo.class.getClassLoader().getResource("script.js").getPath();

		ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
		engine.eval(new FileReader(path));
		Invocable invocable = (Invocable) engine;
		Object result = invocable.invokeFunction("loadJs");
		System.out.println(result);
		System.out.println(result.getClass());
	}

}
